package cn.com.lyj6851.modules.shop.controller;

import cn.com.lyj6851.domain.PageResult;
import cn.com.lyj6851.dozer.service.IGenerator;
import cn.com.lyj6851.logging.aop.AopLog;
import cn.com.lyj6851.modules.aop.ForbidSubmit;
import cn.com.lyj6851.modules.product.domain.YdyStoreProductRelation;
import cn.com.lyj6851.modules.product.service.YdyStoreProductRelationService;
import cn.com.lyj6851.modules.product.service.dto.YdyStoreProductRelationDto;
import cn.com.lyj6851.modules.product.service.dto.YdyStoreProductRelationQueryCriteria;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;

/**
 *
 */
@AllArgsConstructor
@Api(tags = "ProductRelation管理")
@RestController
@RequestMapping("/api/ydyStoreProductRelation")
public class YdyStoreProductRelationController {

    private final YdyStoreProductRelationService yxStoreProductRelationService;
    private final IGenerator generator;


    @AopLog("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('admin','ydyStoreProductRelation:list')")
    public void download(HttpServletResponse response, YdyStoreProductRelationQueryCriteria criteria) throws IOException {
        yxStoreProductRelationService.download(generator.convert(yxStoreProductRelationService.queryAll(criteria), YdyStoreProductRelationDto.class), response);
    }

    @GetMapping
    @AopLog("查询ProductRelation")
    @ApiOperation("查询ProductRelation")
    @PreAuthorize("@el.check('admin','ydyStoreProductRelation:list')")
    public ResponseEntity<PageResult<YdyStoreProductRelationDto>> getYdyStoreProductRelations(YdyStoreProductRelationQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(yxStoreProductRelationService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @AopLog("新增ProductRelation")
    @ApiOperation("新增ProductRelation")
    @PreAuthorize("@el.check('admin','ydyStoreProductRelation:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody YdyStoreProductRelation resources){
        return new ResponseEntity<>(yxStoreProductRelationService.save(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @AopLog("修改ProductRelation")
    @ApiOperation("修改ProductRelation")
    @PreAuthorize("@el.check('admin','ydyStoreProductRelation:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody YdyStoreProductRelation resources){
        yxStoreProductRelationService.updateById(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @ForbidSubmit
    @AopLog("删除ProductRelation")
    @ApiOperation("删除ProductRelation")
    @PreAuthorize("@el.check('admin','ydyStoreProductRelation:del')")
    @DeleteMapping
    public ResponseEntity<Object> deleteAll(@RequestBody Long[] ids) {
        Arrays.asList(ids).forEach(id->{
            yxStoreProductRelationService.removeById(id);
        });
        return new ResponseEntity<>(HttpStatus.OK);
    }
}
